﻿using UnityEngine; 
using UnityEditor;
using System.IO;
using System.Collections.Generic;
public class CreateAssetBundle : Editor
{
    /// <summary>
    /// 打印AB的文件名
    /// </summary>
    [MenuItem("AssetBundle/Print ABs Name")]
    static void GetABsNames()
    { 
        string[] names = AssetDatabase.GetAllAssetBundleNames(); //获取所有设置的AssetBundle
        foreach (string name in names)
        {
            Debug.Log("AB name: " + name);
        }
    }
     
    [MenuItem("AssetBundle/Create All AssetBunldes")]
    static void CreateAssetBunldes()
    {
        //Caching.CleanCache();
        EditorUtility.DisplayProgressBar("Create All AssetBunldes", "Create All AssetBunldes", 0);
        FileStream fs0 = File.Open(Application.dataPath + "/Editor/ABsList.txt", FileMode.Open);
        List<string> files = new List<string>();
        using (StreamReader stream = new StreamReader(fs0))
        {
            while (!stream.EndOfStream)
            {
                files.Add(stream.ReadLine());
            }
        }
         
        string targetPath = Application.dataPath + "/StreamingAssets";
        if (!Directory.Exists(targetPath))
        {
            Directory.CreateDirectory(targetPath);
        }

        foreach (string fileName in files)
        { 
            if(fileName.Trim()=="")
            {
                continue; 
            }

            //Debug.Log("fileName: " + fileName.Trim());

            AssetImporter importer = AssetImporter.GetAtPath("Assets/" + fileName.Trim());
          
            if(fileName.Contains("mp4"))
            {
                File.Copy(Application.dataPath + "/" + fileName, targetPath + "/" + GetFileRealName(fileName) + ".mp4");

                continue;
            }
             
            importer.assetBundleName = GetFileRealName(fileName);
            importer.assetBundleVariant = ""; 
        }

        //刷新资源
        AssetDatabase.Refresh();        
        BuildPipeline.BuildAssetBundles(targetPath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android);
        ////刷新编辑器
        CreateMD5List.Execute(targetPath);

        EditorUtility.ClearProgressBar();
    }

    /// <summary>
    /// 清除所有AB name
    /// </summary>
    [MenuItem("AssetBundle/Clear all ABs name")]
    static void ClearABsName()
    {
        EditorUtility.DisplayProgressBar("Clear all ABs name", "Clear all ABs name", 0);
        string[] ABsNames = AssetDatabase.GetAllAssetBundleNames();
        for (int j = 0; j < ABsNames.Length; j++)
        {
            AssetDatabase.RemoveAssetBundleName(ABsNames[j], true);
        }
        //刷新资源
        AssetDatabase.Refresh();
        EditorUtility.ClearProgressBar();
    }

    /// <summary>
    /// 获取文件名（不包含后缀）
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>
    static string GetFileRealName(string filePath)
    {
        //去除空格
        filePath = filePath.Trim();
        //Debug.Log(filePath); 
        string[] temp0 = filePath.Split('/');
        string temp1 = temp0[temp0.Length - 1];
        string[] temp2 = temp1.Split('.');
        //Debug.Log(temp2[0]);
        return temp2[0];
    }
}
